package com.yuanchu.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanchu.common.utils.PageUtils;
import com.yuanchu.common.utils.Query;
import com.yuanchu.goods.dao.YoshopGoodsSpecRelDao;
import com.yuanchu.goods.dto.SpecData;
import com.yuanchu.goods.entity.YoshopGoodsSpecRelEntity;
import com.yuanchu.goods.entity.YoshopSpecEntity;
import com.yuanchu.goods.service.YoshopGoodsSpecRelService;
import com.yuanchu.goods.service.YoshopSpecService;
import com.yuanchu.goods.service.YoshopSpecValueService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;


@Service("yoshopGoodsSpecRelService")
public class YoshopGoodsSpecRelServiceImpl extends ServiceImpl<YoshopGoodsSpecRelDao, YoshopGoodsSpecRelEntity> implements YoshopGoodsSpecRelService {

    @Resource
    private YoshopSpecService yoshopSpecService;
    @Resource
    private YoshopSpecValueService specValueService;;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<YoshopGoodsSpecRelEntity> page = this.page(
                new Query<YoshopGoodsSpecRelEntity>().getPage(params),
                new QueryWrapper<YoshopGoodsSpecRelEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SpecData.SpecList> getSpecListById(Integer goodsId) {
        List<YoshopGoodsSpecRelEntity> specRels = this.list(new QueryWrapper<YoshopGoodsSpecRelEntity>().eq("goods_id", goodsId));
        List<SpecData.SpecList> specList = new ArrayList<>();
        if (CollectionUtils.isEmpty(specRels)){
           return Collections.emptyList();
        }
        Set<Integer> specs = new HashSet<>();
        specRels.forEach(specRel -> specs.add(specRel.getSpecId()));
        List<Integer> specIds = new ArrayList<>(specs);
        for (int i = 0; i < specIds.size(); i++) {
            SpecData.SpecList spec = new SpecData.SpecList();
            spec.setKey(i);
            spec.setSpecName(this.getSpecNameById(specIds.get(i)));
            spec.setSpecId(specIds.get(i));
            spec.setValueList(this.getSpecValueListById(specIds.get(i), spec.getKey(), goodsId));
            specList.add(spec);
        }
        return specList;
    }

    /**
     * 根据specId获取specValueList
     *
     * @param specId specId
     * @param key
     * @return
     */
    private List<SpecData.SpecList.ValueList> getSpecValueListById(Integer specId, Integer key, Integer goodsId) {
        // List<YoshopSpecValueEntity> specValues = specValueService.list(new QueryWrapper<YoshopSpecValueEntity>()
        //         .eq("spec_id", specId));
        LambdaQueryWrapper<YoshopGoodsSpecRelEntity> lqw = new LambdaQueryWrapper<>();
        lqw.eq(YoshopGoodsSpecRelEntity::getSpecId, specId);
        lqw.eq(YoshopGoodsSpecRelEntity::getGoodsId, goodsId);
        List<YoshopGoodsSpecRelEntity> specRels = this.list(lqw);
        if (CollectionUtils.isEmpty(specRels)){
            return Collections.emptyList();
        }
        List<SpecData.SpecList.ValueList> valueList = new ArrayList<>();
        for (int i = 0; i < specRels.size(); i++) {
            YoshopGoodsSpecRelEntity specRel = specRels.get(i);
            SpecData.SpecList.ValueList value = new SpecData.SpecList.ValueList();
            value.setKey(i);
            value.setGroupKey(key);
            value.setSpecValueId(String.valueOf(specRel.getSpecValueId()));
            value.setSpecValue(specValueService.getById(specRel.getSpecValueId()).getSpecValue());
            valueList.add(value);
        }
        return valueList;

    }

    /**
     * 根据specId获取specName
     * @param specId specId
     * @return
     */
    private String getSpecNameById(Integer specId) {
        YoshopSpecEntity spec = yoshopSpecService.getById(specId);
        return spec.getSpecName();
    }

}